package cn.jinhe.doc.view.action;

import java.util.Date;
import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;

import cn.jinhe.doc.base.BaseAction;
import cn.jinhe.doc.domain.Plan;
import cn.jinhe.doc.domain.Step;
import cn.jinhe.doc.util.QueryHelper;

@Controller
@Scope("prototype")
public class PlanAction extends BaseAction<Plan> {

	private Long userId;

	private Long planId;

	public Long getPlanId() {
		return planId;
	}

	public void setPlanId(Long planId) {
		this.planId = planId;
	}

	public String show() throws Exception {
		// 准备数据:topic
		Plan plan = planService.getById(model.getId());
		ActionContext.getContext().put("plan", plan);

		List<Step> stepList = stepService.findByPlan(plan);
		ActionContext.getContext().put("stepList", stepList);

		return "show";

	}

	public String list() throws Exception {
		new QueryHelper(Plan.class, "p").preparePageBean(planService, pageNum,
				pageSize);

		return "list";
	}

	/*
	 * 删除
	 */
	public String delete() throws Exception {
		planService.delete(model.getId());
		return "toList";
	}

	public String deleteIner() throws Exception {
		planService.delete(planId);
		return "toList";
	}

	/*
	 * 添加
	 */
	public String add() throws Exception {
		model.setAuthor(getCurrentUser());
		model.setFinished(0);
		model.setUnfinished(0);
		int zero = (int) 0;
		model.setProcess(zero);
		model.setPostTime(new Date());
		planService.save(model);
		return "toList";
	}

	/*
	 * 添加页面
	 */
	public String addUI() throws Exception {

		return "saveUI";
	}

	/*
	 * 修改页面
	 */
	public String editUI() throws Exception {
		Plan plan = planService.getById(model.getId());
		ActionContext.getContext().getValueStack().push(plan);
		return "saveUI";
	}

	/*
	 * 修改
	 */
	public String edit() throws Exception {
		Plan plan = planService.getById(model.getId());

		plan.setTitle(model.getTitle());
		plan.setContent(model.getContent());

		planService.update(plan);
		return "toList";
	}

	public Long getUserId() {
		return userId;
	}

	public void setUserId(Long userId) {
		this.userId = userId;
	}

}
